$(document).ready(function () {
    $.ajax({
        type: "GET",
        url: SERVER_URL+"/user/email/"+sessionStorage.getItem("userEmail"),
        async: true,
        dataType: "json",
        success: function (data) {
            document.getElementById("userName").innerText = data.data.firstName+" "+data.data.lastName;
        }
    });

    // 进行中: <a class="btn btn-warning btn-circle btn-sm"><i class="fas fa-angle-double-right"></i></a >
    //
    // 成功: <a class="btn btn-success btn-circle btn-sm"><i class="fas fa-check"></i></a>
    //
    // 失败: <a class="btn btn-danger btn-circle btn-sm"><i class="fas fa-exclamation"></i></a >

    $.ajax({
        type: "GET",
        url: SERVER_URL+"/appointment/email/"+sessionStorage.getItem("userEmail"),
        async: true,
        dataType: "json",
        success: function (data) {
            if (data.data.length === 0){
                alert("No Appointment")
            }else {
                let str = '';
                for (i=0;i<data.data.length;i++){
                    str += '<tr>' +
                        '<td>'+data.data[i].department+'</td><td>'+data.data[i].doctor+'</td><td>'+data.data[i].dateTime+'</td>';
                    if(data.data[i].status == 0){
                        str += '<td>Aeserved &ensp; <a class="btn btn-warning btn-circle btn-sm"><i class="fas fa-angle-double-right"></i></a ></td>' ;
                        str += '<td><a href="#" onclick="jumpToDetail()" class="btn btn-light btn-icon-split"><span class="icon text-gray-600"><i class="fas fa-arrow-right"></i></span><span class="text">View</span></a></td>'+
                            '</tr>'
                    }else {
                        str += '<td>Archive &ensp;<a class="btn btn-success btn-circle btn-sm"><i class="fas fa-check"></i></a></td>' ;
                        str += '<td><a href="#" onclick="jumpToDetail()" class="btn btn-light btn-icon-split disabled"><span class="icon text-gray-600"><i class="fas fa-arrow-right"></i></span><span class="text">View</span></a></td>'+
                            '</tr>'
                    }
                }
                $("#appointmentStatus").html(str);
            }
        }
    });
})


function showAllAppointment() {
    let dateToSearch = document.getElementById("dateToSearch").value;
    console.log(dateToSearch);
    $.ajax({
        type: "GET",
        url: SERVER_URL+"/appointment/date/"+dateToSearch+" 00:00",
        async: true,
        dataType: "json",
        success: function (data) {
            if (data.data.length === 0){
                alert("No Appointment")
            }else {
                let str = '';
                for (i=0;i<data.data.length;i++){
                    str += '<tr>' +
                        '<td>'+data.data[i].department+'</td><td>'+data.data[i].doctor+'</td><td>'+data.data[i].dateTime+'</td>';
                    if(data.data[i].status === 0){
                        str += '<td><a href="#" class="btn btn-success btn-icon-split"><span class="icon text-white-50"><i class="fas fa-check"></i></span><span class="text">Doing</span></a></td>' ;
                    }else {
                        str += '<td><a href="#" class="btn btn-success btn-icon-split"><span class="icon text-white-50"><i class="fas fa-check"></i></span><span class="text">Down</span></a></td>' ;
                    }
                        str += '<td><a href="#" class="btn btn-light btn-icon-split"><span class="icon text-gray-600"><i class="fas fa-arrow-right"></i></span><span class="text">View</span></a></td>'+
                        '</tr>'
                }
                $("#appointmentStatus").html(str);
            }
        }
    });
}

function jumpToDetail() {

}